package com.jiulan.hearthearing.ui.course.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseActivity;
import com.jiulan.hearthearing.constants.UrlConstants;
import com.jiulan.hearthearing.http.HttpRequest;
import com.jiulan.hearthearing.http.HttpStringCallBack;
import com.jiulan.hearthearing.ui.course.adapter.CourseDetailsHudongAdapter;
import com.jiulan.hearthearing.ui.course.model.CourseDetailsHudongModel;
import com.jiulan.hearthearing.ui.course.model.CourseDetailsModel;
import com.jiulan.hearthearing.widget.SpaceVerticalItemDecoration;

import java.util.HashMap;

/**
 * 文稿
 */
public class CourseDetailsFMWengaoActivity extends BaseActivity {
    private int id;
    private CourseDetailsModel courseDetailsModel;
    private CourseDetailsHudongModel model;
    private CourseDetailsHudongAdapter adapter;

    private TextView tvTitle;
    private TextView tvShortTitle;
    private TextView tvTeacherName;

    private TextView tvContent;
    private TextView tvCommentNum;
    private RecyclerView rcvComment;
    private EditText etSay;
    private LinearLayout llShare;
    private LinearLayout llCollect;
    private LinearLayout llHuancun;

    public static void actionStart(Context context, int id, String courseInfo) {
        Intent intent = new Intent(context, CourseDetailsFMWengaoActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("courseInfo", courseInfo);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_course_details_fm_wengao;
    }

    @Override
    protected void initView() {
        setTitleText("文稿");
        id = getIntent().getIntExtra("id", 0);
        courseDetailsModel = new Gson().fromJson(getIntent().getStringExtra("courseInfo"), CourseDetailsModel.class);
        tvTitle = findViewById(R.id.tvTitle);
        tvShortTitle = findViewById(R.id.tvShortTitle);
        tvTeacherName = findViewById(R.id.tvTeacherName);
        tvContent = findViewById(R.id.tvContent);
        tvCommentNum = findViewById(R.id.tvCommentNum);
        rcvComment = findViewById(R.id.rcvComment);
        etSay = findViewById(R.id.etSay);
        llShare = findViewById(R.id.llShare);
        llCollect = findViewById(R.id.llCollect);
        llHuancun = findViewById(R.id.llHuancun);
        rcvComment.setLayoutManager(new LinearLayoutManager(getContext()));
        rcvComment.addItemDecoration(new SpaceVerticalItemDecoration(getContext(), R.dimen.dp_12));
        setView();
    }

    private void setView() {
        tvTitle.setText(courseDetailsModel.getData().getCourse().getTitle());
        tvShortTitle.setText(courseDetailsModel.getData().getCourse().getShort_title());
        tvTeacherName.setText("讲解人：" + courseDetailsModel.getData().getLecturer_info().getName());
        tvContent.setText(courseDetailsModel.getData().getCourse().getShort_content());
    }

    private void getData() {
        new HttpRequest(getContext()).doGet(UrlConstants.course_comment_list + String.valueOf(id), "", new HashMap(), CourseDetailsHudongModel.class, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                if (result instanceof CourseDetailsHudongModel) {
                    model = (CourseDetailsHudongModel) result;
                    setCommentView();
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    private void setCommentView() {
        tvCommentNum.setText("(" + String.valueOf(model.getData().size()) + ")");
        adapter = new CourseDetailsHudongAdapter(model.getData());
        rcvComment.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        getData();
    }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_DEFAULT;
    }
}
